//
// Created by Semir on 2020/3/28.
//

#include <iostream>
#include <deque>
#include <string>
#include <set>

using namespace std;

const int MAX_RULE_NUN = 8;

static string strA, strB;
static string strAS[MAX_RULE_NUN], strBS[MAX_RULE_NUN];
int num;
int minStep;
set<string> sset;

int bfs() {
    deque<string> squeue;
    deque<int> ctnqueue;
    squeue.push_back(strA);
    ctnqueue.push_back(0);
    while (!squeue.empty()) {
        string s = squeue.front();
        int count = ctnqueue.front();
        squeue.pop_front();
        ctnqueue.pop_front();
        if (count > 10) {
            continue;
        }
        if (sset.count(s) > 0) {
            continue;
        }
        sset.insert(s);
        if (s == strB) {
            if (count - 1 < minStep || minStep < 0) {
                minStep = count - 1;
            }
        }
        for (int i = 0; i < num; i++) {
            int index = s.find(strAS[i]);
            if (index >= 0) {
                string newS = s.substr(0, index) + strBS[i] + s.substr(index + strAS[i].length());
                squeue.push_back(newS);
                ctnqueue.push_back(count);
            }
        }
    }
    return minStep;
}

int main() {
    cin >> strA >> strB;
    cin.get();
    num = 0;
    string cinstr;
//    while (true) {
//        getline(cin, cinstr, '\n');
//        if (cinstr == "") {
//            break;
//        }
//        int index = cinstr.find(' ');
//        strAS[num] = cinstr.substr(0, index);
//        strBS[num] = cinstr.substr(index + 1);
//        num++;
//    }
    while (cin >> strAS[num] >> strBS[num]) {
        num++;
    }
    minStep = -1;
    int res = bfs();
    if (res < 0) {
        cout << "NO ANSWER!" << endl;
    } else {
        cout << res << endl;
    }


    return 0;
}
